package com.kr.weitao.util.rest.query;

import com.alibaba.fastjson.JSONObject;
import com.kr.weitao.exception.CommonException;
import com.kr.weitao.util.StringUtils;
import com.kr.weitao.util.rest.dataengine.DataDisposeEngine;
import com.kr.weitao.util.rest.datatrigger.DataBDTrigger;
import com.kr.weitao.util.rest.schema.Function;
import com.kr.weitao.util.rest.schema.FunctionManager;
import com.kr.weitao.util.spring.SpringUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/** Created by paco on 2017/06/13. */
@Log4j2
@Component
@Scope("prototype")
public class ObjectDeleteUtil {
  @Autowired
  @Qualifier("mySQLDataEngine")
  DataDisposeEngine de;

  @Autowired private FunctionManager tableManager;

  @Transactional
  public int execute(JSONObject param) throws CommonException {
    long id = 0; //要删除的记录ID
    int count = 0; //删除记录量
    Function table = null;
    String tableDBName = null;

    //region 参数判断
    if (param == null || param.isEmpty()) {
      throw new CommonException("-1", "请选择要删除的数据");
    }

    tableDBName = param.getString("function");
    if (StringUtils.isNull(tableDBName)) {
      log.error("data table is null:" + param);
      throw new CommonException("-3", "请选择要操作的菜单");
    }

    table = tableManager.getByName(tableDBName);
    if (table == null) {
      log.error("data table object is null:" + tableDBName);
      throw new CommonException("-3", "请选择要操作的菜单");
    }

    id = param.getLongValue(table.getPK().getDb_name().toLowerCase());
    if (id <= 0) {
      //log.error("delete index:"+i+" id error param:"+deleteja.toJSONString());
      throw new CommonException("-1", "要删除的数据不正确");
    }
    //endregion

    //region BD处理
    String bdclassname = table.getDataBDClassName();
    if (table.getIsBd() && StringUtils.isNotNull(bdclassname)) {
      DataBDTrigger dataBDTrigger = null;
      try {
        Class clazz=Class.forName(bdclassname);
        if(clazz.isAnnotationPresent(Component.class)){
          dataBDTrigger = (DataBDTrigger) SpringUtils.getBean(clazz);
        }else{
          dataBDTrigger = (DataBDTrigger) clazz.newInstance();
        }
      } catch (Exception e) {
        log.error("get DataBCTrigger instance error:" + e.getLocalizedMessage(), e);
        throw new CommonException("-80", "BM 配置有误");
      }

//      JSONObject deleteParam = new JSONObject();
//      deleteParam.put(table.getPK().getDb_name().toLowerCase(), id);
//      dataBDTrigger.execute(table, deleteParam);
      dataBDTrigger.execute(table, param);
    }
    //endregion

    //region 数据删除
    StringBuffer deleteSql = null;
    deleteSql = new StringBuffer("delete from ").append(table.getRealFunctionName());
    deleteSql.append(" where ").append(table.getPK().getDb_name());
    deleteSql.append(" =?");

    count = de.delete(deleteSql.toString(), new Object[] {id});
    //endregion

    return count;
  }
}
